import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/Home'),
    redirect: '/article',
    children: [
      { path: 'article', component: () => import('@/views/Layout/Article') },
      { path: 'collect', component: () => import('@/views/Layout/Collect') },
      { path: 'like', component: () => import('@/views/Layout/Like') },
      { path: 'user', component: () => import('@/views/Layout/User') }
    ]
  },
  { path: '/detail/:id', component: () => import('@/views/Detail') },
  { path: '/register', component: () => import('@/views/Register') },
  { path: '/login', component: () => import('@/views/Login') }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === 'register') {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
